import { Component } from '@angular/core';
import { NavController, NavParams,  ToastController } from 'ionic-angular';
import {LoginDataProvider} from '../../providers/login-data/login-data';
import { User } from '../../providers/login-data/user';
import { Storage } from '@ionic/storage';
import { TabsPage } from '../tabs/tabs';
/**
 * Generated class for the LoginPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {

  user:User;
  constructor(public navCtrl: NavController, public navParams: NavParams,
    public totastCtrl:ToastController,
    public loginDataProvider:LoginDataProvider,
    public storage:Storage) {
  }

  ionViewDidLoad() {
    
  }
/**
 * 登录
 * @param username 
 * @param password 
 * @param remember 
 */
  login(username:HTMLInputElement,password:HTMLInputElement,remember:HTMLInputElement){
    let _username=username.value.trim();
    let _password=password.value.trim();
    let _remember=remember.value;
    if(_username.length===0){
      this.showToast('botom','请输入账号');
      return;
    }
    if(_password.length===0){
      this.showToast('botom','请输入密码');
      return;
    }
   this.loginDataProvider.login(_username,_password).toPromise()
   .then(res=>{
    console.log('登录成功');
    console.log(res);
    this.user=res;
    return true;
   },e=>{
     console.log('登录失败');
     console.log(e);
     return false;
   })
   .then(res=>{
     if(res){
       let data={username:this.user.username,objectId:this.user.objectId,
      sessionToken:this.user.sessionToken,
    remember:_remember};
    //记住密码
    if(_remember){
      this.storage.remove('USER_INFO');
      this.storage.set('USER_INFO',JSON.stringify(data));
    }
    this.storage.set('LOGIN_INFO',JSON.stringify(data));
    this.navCtrl.setRoot(TabsPage);
     }else{
       this.showToast('bottom','用户名或密码错误');
     }
   });
  }
  /**
   * 注册
   * @param username 
   * @param password 
   */
  register(username:HTMLInputElement,password:HTMLInputElement){
    let _username=username.value.trim();
    let _password=password.value.trim();
    
    if(_username.length===0){
      this.showToast('botom','账号不能为空');
      return;
    }
    if(_password.length===0){
      this.showToast('botom','密码不能为空');
      return;
    }
    this.loginDataProvider.register(_username,_password).toPromise()
   .then(res=>{
    console.log('注册成功');
    console.log(res);
    this.user=res;
    return true;
   },e=>{
     console.log('注册失败');
     console.log(e);
     return false;
   })
   .then(res=>{
    if(res){
      this.showToast('bottom','注册成功');
      this.navCtrl.setRoot(this.navCtrl.getActive().component);
    }else{
      this.showToast('bottom','账号已存在！');
    }
   });
  }
  /**
   * 提示
   */
  showToast(position:string,message:string){
    let toast=this.totastCtrl.create({
      message:message,
      duration:2000,
      position:position
    });
    toast.present();
  }
}
